#!/bin/bash

PLI_OPTS="-P ${VERDI_HOME}/share/PLI/VCS/LINUX64/novas.tab"
PLI_OPTS="$PLI_OPTS ${VERDI_HOME}/share/PLI/VCS/LINUX64/pli.a"

VXX_OPTS="-full64"
VHD_OPTS=""
VLOG_OPTS="-timescale=1ns/10ps -sverilog -assert svaext" 

# dpi/c settings
DPI_OPTS=""

VCS_OPTS="-LDFLAGS -Wl,-no-as-needed"
#VCS_OPTS="-cc gcc-4.8 -cpp g++-4.8 -LDFLAGS -Wl,-no-as-needed"

VCS_OPTS="$VCS_OPTS +notimingchecks +nospecify +error+30" 
# coverage mode settings
VCM_OPTS="-cm line+fsm+tgl+cond "
VCS_OPTS="$VCM_OPTS $VCS_OPTS -debug_access+all -lca -kdb -fsdb"

# options for simv
SIMV_OPTS=""

if [[ "$1" == "-opts" ]]; then
	echo "$VXX_OPTS $VLOG_OPTS $VCS_OPTS"
fi

if [[ "$1" == "-uvm-opts" ]]; then
	echo "$VXX_OPTS $VLOG_OPTS -uvm-1.2 $VCS_OPTS"
fi

if [[ "$1" == "-simv-opts" ]]; then
	echo "$SIMV_OPTS"
fi

if [[ "$1" == "-list" || "$1" == "-env" ]]; then
	top_file="./tb.v"
	top_name="tb"
	[[ ! -z "$2" ]] && top_file=$2
	[[ ! -z "$3" ]] && top_name=$3
	[[ $ROOT == "" ]] && ROOT=..

	echo "+libext+.v+.sv+.vlib" > file.vc
	echo "" >> file.vc
	for fd in $(find $ROOT -type d) ; do
		fv=$(find $fd -maxdepth 1 -type f -iname "*.v" -or -iname "*.sv" -or -iname "*.vh" -or -iname "*.svh")
		if [[ ! -z "$fv" ]]; then
			echo "-y $fd" >> file.vc
			echo "+incdir+$fd" >> file.vc
			echo "" >> file.vc
		fi
	done

	if [[ -f "$top_file" ]]; then
		echo "" >> file.vc
		echo "$top_file" >> file.vc
	fi
fi

if [[ "$1" == "-env" ]]; then
	echo "#################################" > makefile
	echo "# auto generated file!!" >> makefile
	echo "# please do not edit here!!" >> makefile
	echo "#################################" >> makefile
	echo "" >> makefile
	echo 'BUILD_OPTS = $(shell vcs-gen -opts)' >> makefile
	echo 'RUN_OPTS = $(shell vcs-gen -simv-opts)' >> makefile
	echo "" >> makefile
	echo "default: run" >> makefile
	echo "" >> makefile
	echo "build: file.vc" >> makefile
	echo -e "\tvcs \$(BUILD_OPTS) -f file.vc -top $top_name | tee build.log" >> makefile
	echo "" >> makefile
	echo "run: build" >> makefile
	echo -e "\t./simv \$(RUN_OPTS) | tee run.log" >> makefile
	echo "" >> makefile
	echo "" >> makefile
	echo "verdi: simv.daidir" >> makefile
	echo -e "\tverdi -dbdir simv.daidir &" >> makefile
	echo "" >> makefile
	echo "clean:" >> makefile
	echo -e "\t@rm -rf *.log *.key *.fsdb" >> makefile
	echo -e "\t@rm -rf simv.* work.* novas.*" >> makefile
	echo -e "\t@rm -rf vericomLog verdiLog csrc vc_hdrs.h simv" >> makefile
	echo "" >> makefile
	echo ".PHONY: run build verdi clean" >> makefile
fi

